<?php
/**
 * user.class.php 用户类
 *
 *
 */
include_once dirname(dirname(__FILE__)).'/database.class.php';
class user extends database {

    protected $table = 'users';

    protected $all = '*';

    protected $del = 'del ="1"';

    protected $field = 'u_user';

    /**
     * 构造函数
     * 继承父类构造函数
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 查找是否存在
     * @param $info                  查询的信息
     * @param $sign                  查询的字段
     * @return bool
     */
    public function select_user_exist($info,$sign) {
        $where = $sign.' = "'.$info.'" AND '.$this->del;
        $res = parent::select_exists($this->all,$this->table,$where);
        return $res;
    }

    /**
     * 检查用户名和密码是否正确
     * @param $username 用户名
     * @param $password 密码
     * @return string
     */
    public function check_user($username,$password, $limit='') {
        if($username=='admin' && $password=='admin') {
            return 'admin';
        } else {
            $where = $this->field.'="'.$username.'" AND '.$this->del;
			$info = parent::get_one($this->all,$this->table,$where,$limit);
            if($info['u_pwd']==$password)
                return 'user';
            else
                return 'no';
        }
    }

    /**
     * 增加用户
     * @param $username   用户名
     * @param $password   密码
     * @param $department 部门
     * @return boolean    返回值为布尔型
     */
    public function add_user($username,$password,$department) {
        if($username == 'admin') {
            return false;
        }
        $where = ' d_name="'.$department.'" AND '.$this->del;
        $depart = parent::get_one($this->all,$table='depart',$where);
        $data=array('u_pwd'=>$password,'u_user'=>$username,'d_id'=>$depart['d_id'],'del'=>'1');
        return parent::insert($data,$this->table);
    }

    /**
     * 获取单个用户信息
     * @param $userinfo         查询的信息
     * @param $field            查询字段
     * @param string|限制语句 $limit 限制语句
     * @return array
     */
    public function get_one_user($userinfo, $field) {
        $where = $field.'="'.$userinfo.'" AND '.$this->del;
        $info = parent::get_one($this->all,$this->table,$where);
        return $info;
    }

    /**
     * 获取所有用户信息
     * @param string|查询条件 $where 查询条件
     * @param string|限制语句 $limit 限制语句
     * @return array
     */
    public function get_all_user($where='',$limit='') {
        $where = $where=='' ? $this->del : $where.' AND '.$this->del;
        $info = parent::select($this->all,$this->table,$where,$limit);
        return $info;
    }

    /**
     * 获取总条数
     * @param $where         查询条件
     * @return mixed
     */
    public function get_count_user($where) {
        $num = parent::select_count($this->table,$where);
        return $num;
    }

    /**
     * 更新用户信息
     * @param $id
     * @param $data       数组，内容为要更新的数据
     * @return bool
     */
    public function update_user($id,$data) {
        $where = ' u_id="'.$id.'"';
        $res = parent::update($data,$this->table,$where);
        return $res;
    }

    /**
     * 删除用户信息
     * @param $uid        用户id
     * @return bool
     */
    public function delete_user($uid) {
        $where = ' u_id="'.$uid.'"';
        $data='del=0';
        $res = parent::update($data,$this->table,$where);
        return $res;
    }

    /**
     * 添加session信息
     * @param $username   用户名
     */
    public function add_session($username) {
        $info=self::get_one_user($username,'u_user');
        $_SESSION["u_user"]=$info['u_user'];
        $_SESSION["u_id"]=$info['u_id'];
    }

    /**
     * 删除session
     */
    public function destroy_session() {
        $_SESSION = array();
        session_destroy();
    }

    /**
     * 注销登录
     */
    public function quit($username) {
        $_SESSION = array();
        session_destroy();
		if($username!= 'admin'){
			$out = '<script>location.href="http://localhost/work/login.php"</script>';
			echo $out;
		}
		else{
			$quit = '<script>location.href="http://localhost/work/login1.php"</script>';
			echo $quit;
		}
    }
	
}









?>